package com.hzh.cusviewdemo.Presenter;

import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hzh.cusviewdemo.Base.ApiResponse;
import com.hzh.cusviewdemo.Base.ApiService;
import com.hzh.cusviewdemo.Base.LoadAction;
import com.hzh.cusviewdemo.Bean.MusicData;
import com.hzh.cusviewdemo.Bean.UpdateData;
import com.hzh.cusviewdemo.Util.Config;
import com.hzh.cusviewdemo.Util.DialogUtils;
import com.hzh.cusviewdemo.Util.RSAUtil;
import com.hzh.cusviewdemo.Util.RetrofitUtils;
import com.kongzue.dialogx.dialogs.WaitDialog;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public interface MusicPresenter extends LoadAction {

    interface OnSearchMusicListener {
        void onGetMusicSuccess(List<com.hzh.cusviewdemo.Bean.MusicSearchData> result);

        void onGetMusicFail(String msg);
    }

    interface OnCheckKeyListener {
        void onCheckOk(String result);

        void onCheckFail(String msg);
    }

    interface OnCheckUpdateListener {
        void onNeedUpdate(UpdateData data);
    }

    interface OnListenMusicListener {
        void onListenSuccess(com.hzh.cusviewdemo.Bean.MusicInfoData result);

        void onListenFail(String msg);
    }

    public interface OnMusicListListener {
        void onGetMusicSuccess(List<MusicResponse.AbsData> result);

        void onGetMusicFail(String msg);
    }

    public interface OnMusicUrlListener {
        void onGetUrlSuccess(String musicUrl);

        void onGetUrlFail(String msg);
    }

    public interface OnMusicInfoListener {
        void onGetInfoSuccess(MusicInfoData musicInfoData);

        void onGetInfoFail(String msg);
    }

    public interface OnMusicCommentListener {
        void onGetCommentSuccess(MusicCommentData musicCommentData);

        void onGetCommentFail(String msg);
    }

    public interface OnAddMusicDataListener {
        void OnAddMusicDataSuccess();

        void OnAddMusicDataFail(String msg);
    }

    // 关于Music的网络操作
    default void getMusicList(Context context, OnMusicListListener listener) {
        getMusicList(context, "", listener);
    }

    /***********************************************************************************************************************/

    /**
     * 搜索 NEW
     *
     * @param songName
     * @param listener
     */
    default void searchMusic(String songName, OnSearchMusicListener listener) {
        WaitDialog.show("等一下，找歌中...");
        new Thread(() -> {
            if (songName.equals("")) {
                // 随便找歌曲
                ToastUtils.showShort("请输入歌曲名");
                WaitDialog.dismiss();
            } else {
                ApiService service = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class);
                service.searchMusic(songName)
                        .enqueue(new Callback<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>>() {
                            @Override
                            public void onResponse(Call<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>> call, Response<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>> response) {
                                if (response.body() == null) {
                                    ToastUtils.showShort("没有搜索结果~");
                                    WaitDialog.dismiss();
                                    return;
                                }
                                List<com.hzh.cusviewdemo.Bean.MusicSearchData> musicInfoDataList = response.body().getData();
                                if (musicInfoDataList.size() == 0) {
                                    //无搜索结果
                                    listener.onGetMusicFail("没有搜索结果~");
                                    WaitDialog.dismiss();
                                    return;
                                }
                                listener.onGetMusicSuccess(musicInfoDataList);
                                WaitDialog.dismiss();
                            }

                            @Override
                            public void onFailure(Call<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>> call, Throwable t) {
                                listener.onGetMusicFail("获取歌曲失败~");
                                WaitDialog.dismiss();
                            }
                        });
            }
        }).start();
    }

    /**
     * 分页搜索歌曲
     *
     * @param songName
     * @param pageNum  页码
     * @param pageSize 单页数量
     * @param listener
     */
    default void searchMusicPage(String songName, String pageNum, String pageSize, OnSearchMusicListener listener) {
        WaitDialog.show("等一下，找歌中...");
        new Thread(() -> {
            if (songName.equals("")) {
                // 随便找歌曲
                ToastUtils.showShort("请输入歌曲名");
                WaitDialog.dismiss();
            } else {
                ApiService service = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class);
                service.searchMusicPage(songName, pageNum, pageSize)
                        .enqueue(new Callback<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>>() {
                            @Override
                            public void onResponse(Call<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>> call, Response<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>> response) {
                                if (response.body() == null) {
                                    ToastUtils.showShort("没有搜索结果~");
                                    WaitDialog.dismiss();
                                    return;
                                }
                                List<com.hzh.cusviewdemo.Bean.MusicSearchData> musicInfoDataList = response.body().getData();
                                if (musicInfoDataList.size() == 0) {
                                    //无搜索结果
                                    listener.onGetMusicFail("没有搜索结果~");
                                    WaitDialog.dismiss();
                                    return;
                                }
                                listener.onGetMusicSuccess(musicInfoDataList);
                                WaitDialog.dismiss();
                            }

                            @Override
                            public void onFailure(Call<ApiResponse<List<com.hzh.cusviewdemo.Bean.MusicSearchData>>> call, Throwable t) {
                                listener.onGetMusicFail("获取歌曲失败~");
                                WaitDialog.dismiss();
                            }
                        });
            }
        }).start();
    }

    default void listenMusic(Context context, String musicId, OnListenMusicListener listener) {
        showLoading(context);
        new Thread(() -> {
            if (musicId.equals("")) {
                // 随便找歌曲
                ToastUtils.showShort("异常！");
            } else {
                ApiService service = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class);
                service.listenMusic(musicId)
                        .enqueue(new Callback<ApiResponse<com.hzh.cusviewdemo.Bean.MusicInfoData>>() {
                            @Override
                            public void onResponse(Call<ApiResponse<com.hzh.cusviewdemo.Bean.MusicInfoData>> call, Response<ApiResponse<com.hzh.cusviewdemo.Bean.MusicInfoData>> response) {
                                if (response.body() == null) {
                                    ToastUtils.showShort("获取歌曲失败");
                                    showComplete();
                                    return;
                                }
                                com.hzh.cusviewdemo.Bean.MusicInfoData musicInfoData = response.body().getData();
                                if (musicInfoData == null) {
                                    //无搜索结果
                                    ToastUtils.showShort("获取歌曲失败");
                                    showComplete();
                                    return;
                                }
                                listener.onListenSuccess(musicInfoData);
                                showComplete();
                            }

                            @Override
                            public void onFailure(Call<ApiResponse<com.hzh.cusviewdemo.Bean.MusicInfoData>> call, Throwable t) {
                                listener.onListenFail("获取歌曲失败");
                                showComplete();
                            }
                        });
            }
        }).start();
    }

    /**
     * 对Key进行校验
     *
     * @param key
     * @param listener
     */
    default void checkKey(String key, OnCheckKeyListener listener) {
        WaitDialog.show("请稍后~");
        new Thread(() -> {
            if (key.equals("")) {
                // 随便找歌曲
                DialogUtils.showErrorToast("秘钥不能为空");
                WaitDialog.dismiss();
            } else {
                ApiService service = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class);
                service.checkKey(key, DialogUtils.getDeviceCode(), RSAUtil.APP_SET_VERSION)
                        .enqueue(new Callback<ApiResponse<String>>() {
                            @Override
                            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                                if (response.body() == null) {
                                    listener.onCheckFail("校验失败，稍后再试！");
                                    WaitDialog.dismiss();
                                    return;
                                }
                                if (ObjectUtils.isEmpty(response.body().getData())) {
                                    listener.onCheckFail(response.body().getMsg());
                                    WaitDialog.dismiss();
                                    return;
                                }
                                listener.onCheckOk(response.body().getData());
                                WaitDialog.dismiss();
                            }

                            @Override
                            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                                listener.onCheckFail("校验失败，稍后再试！");
                                WaitDialog.dismiss();
                            }
                        });
            }
        }).start();
    }

    /**
     * 校验更新
     *
     * @param listener
     */
    default void checkUpdate(OnCheckUpdateListener listener) {
        WaitDialog.show("检查更新中...");
        new Thread(() -> {
            ApiService service = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class);
            service.checkUpdate("14", AppUtils.getAppVersionName())
                    .enqueue(new Callback<ApiResponse<UpdateData>>() {
                        @Override
                        public void onResponse(Call<ApiResponse<UpdateData>> call, Response<ApiResponse<UpdateData>> response) {
                            if (response.body() == null) {
                                WaitDialog.dismiss();
                                return;
                            }
                            if (ObjectUtils.isEmpty(response.body().getData())) {
                                WaitDialog.dismiss();
                                return;
                            }
                            listener.onNeedUpdate(response.body().getData());
                            WaitDialog.dismiss();
                        }

                        @Override
                        public void onFailure(Call<ApiResponse<UpdateData>> call, Throwable t) {
                            WaitDialog.dismiss();
                        }
                    });

        }).start();
    }


    /***********************************************************************************************************************/


    /**
     * 获取歌曲列表，名称可填
     *
     * @param songName
     */
    default void getMusicList(Context context, String songName, OnMusicListListener listener) {
        showLoading(context);
        new Thread(() -> {
            if (songName.equals("")) {
                // 随便找歌曲
            } else {
                ApiService service = RetrofitUtils.getInstance().getMusicListRetrofit().create(ApiService.class);
                service.getMusicList("0", "50", songName, "music", "1", "1", "web_2013", "kt", "0", "1", "json",
                                "utf8", "1", "1", "mbox", "pc", "MUSIC_9.1.1.2_BCS2", "38668888", "1", "1", "1")
                        .enqueue(new Callback<MusicResponse>() {
                            @Override
                            public void onResponse(Call<MusicResponse> call, Response<MusicResponse> response) {
                                if (response == null) {
                                    listener.onGetMusicFail("获取歌曲失败，网络错误101！");
                                    showComplete();
                                    return;
                                }
                                if (response.body() != null) {
                                    if (response.body().getAbslist() != null) {
                                        if (response.body().getAbslist().size() > 0) {
                                            listener.onGetMusicSuccess(response.body().getAbslist());
                                            showComplete();
                                        } else {
                                            listener.onGetMusicFail("找不到这首歌！");
                                            showComplete();
                                        }
                                    } else {
                                        listener.onGetMusicFail("找不到这首歌！");
                                        showComplete();
                                    }
                                } else {
                                    listener.onGetMusicFail("获取歌曲失败，网络错误102！");
                                    showComplete();
                                }
                            }

                            @Override
                            public void onFailure(Call<MusicResponse> call, Throwable t) {
                                listener.onGetMusicFail("获取歌曲失败，网络错误103！");
                                showComplete();
                            }
                        });
            }
        }).start();
    }

    /**
     * 获取歌曲链接 https://music.163.com/song/media/outer/url?id=436514312
     *
     * @param musicId
     */
    default void getMusicUrl(Context context, String musicId, OnMusicUrlListener listener) {
        showLoading(context);
        new Thread(() -> {
            if (musicId.equals("")) {
                // 随便找歌曲
                listener.onGetUrlFail("歌曲文件获取错误！");
            } else {
                ApiService service = RetrofitUtils.getInstance().getMusicUrlRetrofit().create(ApiService.class);
                service.getMusicUrl("url", musicId, "convert_url3", "128kmp3", "web", "1609141904970", "1", "87e51122-48e1-11eb-adc3-bdfbd8d9306e")
                        .enqueue(new Callback<SearchResponse>() {
                            @Override
                            public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) {
                                if (response == null) {
                                    listener.onGetUrlFail("歌曲文件获取错误，网络错误201！");
                                    showComplete();
                                    return;
                                }
                                if (response.body() != null) {
                                    if (response.body().getCode() == 200) {
                                        listener.onGetUrlSuccess(response.body().getUrl());
                                        showComplete();
                                    } else {
                                        listener.onGetUrlFail("歌曲文件获取错误，网络错误202！");
                                        showComplete();
                                    }
                                } else {
                                    listener.onGetUrlFail("歌曲文件获取错误，网络错误203！");
                                    showComplete();
                                }
                            }

                            @Override
                            public void onFailure(Call<SearchResponse> call, Throwable t) {
                                listener.onGetUrlFail("歌曲文件获取错误，网络错误204！");
                                showComplete();
                            }
                        });
            }
        }).start();
    }

    /**
     * 获取歌曲链接
     *
     * @param musicId
     */
    default void getMusicUrl(String musicId, OnMusicUrlListener listener) {
        new Thread(() -> {
            if (musicId == null) {
                listener.onGetUrlFail("歌曲文件获取错误！");
                return;
            }
            if (musicId.equals("")) {
                // 随便找歌曲
                listener.onGetUrlFail("歌曲文件获取错误！");
            } else {
                ApiService service = RetrofitUtils.getInstance().getMusicUrlRetrofit().create(ApiService.class);
                service.getMusicUrl("url", musicId, "convert_url3", "128kmp3", "web", "1609141904970", "1", "87e511b1-48e1-11eb-adc3-bdfbd8d9306e")
                        .enqueue(new Callback<SearchResponse>() {
                            @Override
                            public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) {
                                if (response == null) {
                                    listener.onGetUrlFail("歌曲文件获取错误，网络错误201！");
                                    return;
                                }
                                if (response.body() != null) {
                                    if (response.body().getCode() == 200) {
                                        listener.onGetUrlSuccess(response.body().getUrl());
                                    } else {
                                        listener.onGetUrlFail("歌曲文件获取错误，网络错误202！");
                                    }
                                } else {
                                    listener.onGetUrlFail("歌曲文件获取错误，网络错误203！");
                                }
                            }

                            @Override
                            public void onFailure(Call<SearchResponse> call, Throwable t) {
                                listener.onGetUrlFail("歌曲文件获取错误，网络错误204！");
                            }
                        });
            }
        }).start();
    }

    /**
     * 获取歌曲歌词等信息
     *
     * @param musicId 如:59141878
     */
    default void getMusicInfo(String musicId, OnMusicInfoListener listener) {
        listener.onGetInfoFail("歌曲歌词获取错误！");

//        new Thread(() -> {
//            if (musicId.equals("")) {
//                // 随便找歌曲
//                listener.onGetInfoFail("歌曲歌词获取错误！");
//            } else {
//                ApiService service = RetrofitUtils.getInstance().getMusicInfoRetrofit().create(ApiService.class);
//                // MUSIC_51676792
//                Log.e(Config.TAG, "MP=getMusicInfo=" + musicId.substring(musicId.indexOf("_") + 1));
//                service.getMusicInfo(musicId.substring(musicId.indexOf("_") + 1), "1", "969ba290-4b49-11eb-8db2-ebd372233623")
//                        .enqueue(new Callback<>() {
//                            @Override
//                            public void onResponse(Call<MusicInfoResponse> call, Response<MusicInfoResponse> response) {
//                                if (response == null) {
//                                    listener.onGetInfoFail("歌曲信息获取错误，网络错误301！");
//                                    return;
//                                }
//                                if (response.body() != null) {
//                                    if (response.body().getStatus() == 200) {
//                                        listener.onGetInfoSuccess(response.body().getData());
//                                    } else {
//                                        listener.onGetInfoFail("歌曲信息获取错误，网络错误302！");
//                                    }
//                                } else {
//                                    listener.onGetInfoFail("歌曲信息获取错误，网络错误303！");
//                                }
//                            }
//
//                            @Override
//                            public void onFailure(Call<MusicInfoResponse> call, Throwable t) {
//                                listener.onGetInfoFail("歌曲信息获取错误，网络错误304！");
//                            }
//                        });
//            }
//        }).start();
    }

    /**
     * 获取每日推荐歌曲
     * @param listener
     */
    /**
     * 获取每日推荐歌曲，名称可填
     */
    default void getTodayMusic(Context context, OnMusicListListener listener) {
        showLoading(context);
        new Thread(() -> {
            ApiService service = RetrofitUtils.getInstance().getTodayMusicRetrofit().create(ApiService.class);
            service.getTodayMusic("kwplayer_ar_10.3.1.0", "web", "50", "1", "0", "kwplayer_ar_10.3.1.0_40.apk", "rcm_discover", "android", "1.2.4", "3", "2519728212",
                            "2", "0", "0")
                    .enqueue(new Callback<TodayMusicResponse>() {
                        @Override
                        public void onResponse(Call<TodayMusicResponse> call, Response<TodayMusicResponse> response) {
                            if (response == null) {
                                listener.onGetMusicFail("获取每日推荐歌曲失败，网络错误！");
                                showComplete();
                                return;
                            }
                            if (response.body() != null) {
                                if (response.body().getChild() != null) {
                                    if (response.body().getChild().size() > 0) {
                                        // 数据返回
                                        List<MusicResponse.AbsData> result = new ArrayList<>();
                                        for (TodayMusicData todayMusicData : response.body().getChild()) {
                                            for (TodayMusicDetail musicDetail : todayMusicData.getChild()) {
                                                MusicResponse.AbsData absData = new MusicResponse.AbsData();
                                                absData.setSONGNAME(musicDetail.getTitle());
                                                absData.setALBUM(musicDetail.getAlbum());
                                                absData.setARTIST(musicDetail.getSubtitle());
                                                absData.setMUSICRID(musicDetail.getMusicdata().getMusicrid());
                                                absData.setTpay(String.valueOf(musicDetail.getMusicdata().getTpay()));
                                                result.add(absData);
                                            }
                                        }
                                        listener.onGetMusicSuccess(result);
                                        showComplete();
                                    } else {
                                        getMusicList(context, "民谣", new OnMusicListListener() {
                                            @Override
                                            public void onGetMusicSuccess(List<MusicResponse.AbsData> result) {
                                                listener.onGetMusicSuccess(result);
                                            }

                                            @Override
                                            public void onGetMusicFail(String msg) {
                                                listener.onGetMusicFail("没有歌曲，出错！");
                                            }
                                        });
                                        showComplete();
                                    }
                                } else {
                                    getMusicList(context, "民谣", new OnMusicListListener() {
                                        @Override
                                        public void onGetMusicSuccess(List<MusicResponse.AbsData> result) {
                                            listener.onGetMusicSuccess(result);
                                        }

                                        @Override
                                        public void onGetMusicFail(String msg) {
                                            listener.onGetMusicFail("没有歌曲，出错！");
                                        }
                                    });
                                    showComplete();
                                }
                            } else {
                                listener.onGetMusicFail("获取今日推荐歌曲失败，网络错误！");
                                showComplete();
                            }
                        }

                        @Override
                        public void onFailure(Call<TodayMusicResponse> call, Throwable t) {
                            listener.onGetMusicFail("获取今日推荐歌曲失败，网络错误！");
                            showComplete();
                        }
                    });
        }).start();
    }

    /**
     * 获取歌曲歌词等信息
     *
     * @param musicId 如:59141878
     */
    default void getMusicComment(String musicId, Integer page, OnMusicCommentListener listener) {
        listener.onGetCommentFail("评论数据获取错误，请重新加载！");

//        new Thread(() -> {
//            if (musicId.equals("")) {
//                // 随便找歌曲
//                listener.onGetCommentFail("评论数据获取错误，请重新加载！");
//            } else {
//                ApiService service = RetrofitUtils.getInstance().getMusicCommentRetrofit().create(ApiService.class);
//                service.getMusicComment(
//                                "get_comment", "web", String.valueOf(page), "20", "15", musicId, "0", "newWeb", "1", "98dca7a0-b714-11ed-a362-ef0030d49379")
//                        .enqueue(new Callback<>() {
//                            @Override
//                            public void onResponse(Call<MusicCommentData> call, Response<MusicCommentData> response) {
//                                if (response == null) {
//                                    listener.onGetCommentFail("评论数据获取错误，请重新加载！");
//                                    return;
//                                }
//                                if (response.body() != null) {
//                                    listener.onGetCommentSuccess(response.body());
//                                } else {
//                                    listener.onGetCommentFail("评论数据获取错误，请重新加载！");
//                                }
//                            }
//
//                            @Override
//                            public void onFailure(Call<MusicCommentData> call, Throwable t) {
//                                listener.onGetCommentFail("评论数据获取错误，请重新加载！");
//                            }
//                        });
//            }
//        }).start();
    }

    /**
     * 添加MusicData
     *
     * @param
     */
    default void addMusicData(MusicData data, OnAddMusicDataListener listener) {
        listener.OnAddMusicDataFail("Fail");

//        new Thread(() -> {
//            ApiService service = RetrofitUtils.getInstance().getMyRetrofit().create(ApiService.class);
//            service.addMusicData(data).enqueue(new Callback<>() {
//                @Override
//                public void onResponse(Call<ApiResponse<Object>> call, Response<ApiResponse<Object>> response) {
//                    if (response == null) {
//                        listener.OnAddMusicDataFail("Fail");
//                        return;
//                    }
//                    if (response.body() != null) {
//                        listener.OnAddMusicDataSuccess();
//                    } else {
//                        listener.OnAddMusicDataFail("Fail");
//                    }
//                }
//
//                @Override
//                public void onFailure(Call<ApiResponse<Object>> call, Throwable t) {
//                    listener.OnAddMusicDataFail("Fail");
//                }
//            });
//        }).start();
    }
}
